package com.webapp.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.webapp.domain.entity.Counselor;
import com.webapp.domain.entity.Disputes;
import com.webapp.domain.entity.Session;
import com.webapp.domain.vo.Pagination;
@Repository("sessionDAO")
public class SessionDAO extends AbstractDAO<Session> {

	/**
	 * 根据纠纷查询,咨询师会话
	 * @param disputes
	 * @return
	 */
	public List<Session> findSessionByDisputes(long disputesId){
		return  this.find("where session.disputes.id= ? and session.status!='1'", disputesId);
	}
	
	/**
	 * 根据纠纷与咨询师  查询会话
	 */
	public Session getSessionByDisputesAndUser(long disputesId,long counselorId){
		return this.get("where session.disputes.id=? and session.counselor.id=? ", disputesId,counselorId);
	}
	/**
	 * 根据咨询师查询所有会话
	 * @param counselor
	 * @return
	 */
	public List<Session> findSessionByCounselor(long counselorId){
		return  this.find("where session.counselor.id= ?", counselorId);
	}
	
	/**
	 * 执行sql，返回long
	 * @param countHql
	 * @return
	 */
	public Long getCount(String countHql){
		Object object = getSession().createQuery(countHql).uniqueResult();
		return (Long)object;
	} 
	
	/**
	 * 分页查询咨询师所有的会话，查找分配给他的咨询
	 * @param messageQuery
	 * @param whereQuery
	 * @param counselorId
	 * @param p
	 * @return
	 */
	public Pagination<Session> paginateByQuery(String messageQuery, 
			String whereQuery,Long counselorId, Pagination<Session> p){
		List<Session> results = new ArrayList<Session>();
		Long count = getCount("select count(*) from Session s left join s.disputes d "+whereQuery);
		p.setTotalCount(count);
		String hql = "select s," +
				"(select count(*) from Message m "
				+ " where m.session.id = s.id and m.session.counselor.id = :cid "+messageQuery+") as number"
				+ " from Session s "
				+ " left join fetch s.disputes d "
				+ " left join fetch d.proposer" +whereQuery;
		if(!StringUtils.isEmpty(p.getDirection())){
			if ("desc".equals(p.getDirection())) {
				hql = hql + " order by s."+p.getOrderBy()+" desc";
			}else {
				hql = hql + " order by s."+p.getOrderBy();
			}
		}
		Query query = getSession().createQuery(hql);
		query.setLong("cid", counselorId)
		.setFirstResult(p.getPosStart())
		.setMaxResults(p.getPageSize());
		List<Object[]> s = query.list();
		Session tmp;
		for (Object[] objects : s) {
			tmp = (Session)objects[0];
			tmp.getDisputes().setMessageCount((Long)objects[1]);
			results.add(tmp);
		
		}
		p.setData(results);
		return p;
	}

	public Session fetch(long id) {
		String hql = "select s from Session s " +
				 "left join fetch s.disputes d " +
				 "left join fetch d.user " +
				 "left join fetch s.counselor " +
				 "where s.id = :sid";
		Query query = getSession().createQuery(hql);
		query.setLong("sid", id);
		Session session = (Session) query.uniqueResult();
		return session;
	}
	/**
	 * 该纠纷已选咨询师个数
	 * @param disputesId
	 * @return
	 */
	public int getCountSessionByDisputesId(long disputesId){
		String sql = "select count(0) from SESSION s where s.disputes_id = ? and s.status !='1'";
		SQLQuery query = getSession().createSQLQuery(sql);
		query.setParameter(0, disputesId);
		Object obj = query.uniqueResult();
		return Integer.parseInt(obj.toString());
	}
	
	/**
	 * 查询咨询师未完成任务记录数
	 * @param counselorId
	 * @return
	 */
	public int getCountSession_counselor(long counselorId){
		String sql = "SELECT count(*) FROM SESSION  WHERE  STATUS NOT IN ('1','5') AND counselor_id = ?";
		SQLQuery query = getSession().createSQLQuery(sql);
		query.setParameter(0, counselorId);
		Object obj = query.uniqueResult();
		return Integer.parseInt(obj.toString());
	}
	
	public int updatSessionStatus(long disputesId){
		String sql = "UPDATE SESSION SET STATUS = '5' WHERE DISPUTES_ID = ?";
		SQLQuery query = getSession().createSQLQuery(sql);
		query.setParameter(0, disputesId);
		return query.executeUpdate();
	}
	
}
